package com.yangye.pisystem.service.impl;

import com.yangye.picommon.core.service.impl.BaseServiceImpl;
import com.yangye.pisystem.dao.SysRoleMenuMapper;
import com.yangye.pisystem.domain.SysRoleMenu;
import com.yangye.pisystem.dto.SysRoleMenuDTO;
import com.yangye.pisystem.service.SysRoleMenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yangye
 * @date 2019/8/21
 */
@Service
public class SysRoleMenuServiceImpl extends BaseServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {
	@Override
	@Transactional
	public int updateMenu(SysRoleMenuDTO sysRoleMenuDTO) {
		// 先删除再保存
		SysRoleMenu deleteModel = new SysRoleMenu();
		deleteModel.setRoleId(sysRoleMenuDTO.getRoleId());
		dao.delete(deleteModel);

		List<SysRoleMenu> list = new ArrayList<>();
		List<Long> menuIds = sysRoleMenuDTO.getMenuIds();
		for (Long menuId : menuIds) {
			SysRoleMenu sysRoleMenu = new SysRoleMenu();
			sysRoleMenu.setRoleId(sysRoleMenuDTO.getRoleId());
			sysRoleMenu.setMenuId(menuId);
			list.add(sysRoleMenu);
		}
		return dao.insertList(list);
	}

	@Override
	public List<SysRoleMenu> findByRoleId(Long roleId) {
		SysRoleMenu queryModel = new SysRoleMenu();
		queryModel.setRoleId(roleId);
		return dao.select(queryModel);
	}
}
